package com.thememaker.util;

import com.thememake.Constants;
import com.thememake.entity.ThemeFileInfo;
import com.thememake.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

/**
 * Created by jiajun.jiang on 2017/11/29.
 */
public class ImportScreenUtils {
    Path iuzPath;
    Path itzPath;
    Path lockstylePath;

    public boolean checkFileChooser(File file){
        if(file.getPath().endsWith("unlock")){
            lockstylePath = file.toPath();
            return true;
        }
        Stream<Path> pathStream;
        try {
            pathStream =  FileUtils.iteratorDirToStream(file,false, path -> path.toString().endsWith("lockstyle")||
                    path.toString().endsWith("lockscreen")|| path.toString().endsWith(".itz") || path.toString().endsWith(".iuz"));
            Object[] paths = pathStream.toArray();
            if (paths.length == 2){
                for (Object object: paths) {
                    Path path = (Path) object;
                    if (path.toString().endsWith("iuz")){
                        iuzPath = path;
                    }else{
                        itzPath = path;
                    }
                }
                pathStream.close();
                return true;
            }else{
                pathStream.close();
                for (Object object: paths) {
                    Path path = (Path) object;
                    System.out.println(path.toString());
                    if (path.toString().endsWith("lockstyle") ||  path.toString().endsWith("lockscreen")){
                        lockstylePath = path;
                        return true;
                    }
                }
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public void doImportScreen(ThemeFileInfo themeFileInfo){
        if (themeFileInfo.getThemeType().equals(Constants.VIVO)){
            Path lockscreen25Path = Paths.get(themeFileInfo.getOutputDir().getPath(),"lockscreen","2.5");
            Path lockscreen30Path = Paths.get(themeFileInfo.getOutputDir().getPath(),"lockscreen","3.0");
            doCopy(lockscreen25Path,iuzPath);
            doCopy(lockscreen30Path,itzPath);
        }else{
            try {
                if (Files.exists(lockstylePath)){
                    if (themeFileInfo.getThemeType().equals(Constants.OPPO)){
                        Path lockscreenPath = Paths.get(themeFileInfo.getOutputDir().getPath(),"lockscreen","lockstyle");
                        FileUtils.copyFile(lockstylePath,lockscreenPath,true);
                    }else if(themeFileInfo.getThemeType().equals(Constants.EMUI)){
                        Path unlockPath = Paths.get(themeFileInfo.getOutputDir().getPath(),"unlock");
                        if(unlockPath.toFile().exists()){
                            FileUtils.deleteDir(unlockPath.toFile());
                        }
                        FileUtils.copyDir(lockstylePath.toString(),unlockPath.toString(),true);
                    }
                    else{
                        Path lockscreenPath = Paths.get(themeFileInfo.getOutputDir().getPath(),"lockscreen");
                        FileUtils.copyFile(lockstylePath,lockscreenPath,true);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void doCopy(Path lockscreenPath,Path itzPath){
        for (String densityDir: ImageChangeDensityUtil.densityDirs) {
            Path path = Paths.get(lockscreenPath.toString(),densityDir);
            try {
                ImageChangeDensityUtil.checkDir(path);
                if (itzPath.toString().endsWith("itz")){
                    FileUtils.copyFile(itzPath,Paths.get(path.toString(),"lockscreen.itz"),true);
                }else{
                    FileUtils.copyFile(itzPath,Paths.get(path.toString(),"lockscreen.iuz"),true);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
